<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redis与数据库一致性解决方案</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .solution-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.3);
        }
        .number-badge {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            width: 32px;
            height: 32px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            font-weight: 600;
            flex-shrink: 0;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Redis与数据库<br>一致性解决方案
                </h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">
                    深入探索内存数据库与持久化存储的协同之道
                </p>
                <div class="flex justify-center space-x-4">
                    <div class="flex items-center space-x-2">
                        <i class="fas fa-database text-2xl"></i>
                        <span class="text-lg">数据同步</span>
                    </div>
                    <div class="flex items-center space-x-2">
                        <i class="fas fa-sync-alt text-2xl"></i>
                        <span class="text-lg">事务管理</span>
                    </div>
                    <div class="flex items-center space-x-2">
                        <i class="fas fa-shield-alt text-2xl"></i>
                        <span class="text-lg">故障恢复</span>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Introduction Section -->
    <section class="py-16 bg-white">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap">在</span>实际应用中，常常需要将Redis与数据库（例如关系型数据库）结合使用。由于Redis是一个内存数据库，而传统数据库通常是持久化的，因此在使用这两者时，需要考虑一致性的问题。以下是一些解决Redis和数据库一致性问题的解决方案。
                </p>
            </div>
        </div>
    </section>

    <!-- Architecture Diagram -->
    <section class="py-12 bg-gray-50">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">
                <h2 class="text-3xl font-bold text-center mb-12 text-gradient">系统架构概览</h2>
                <div class="mermaid">
                    graph TB
                        A[应用程序] --> B[Redis缓存]
                        A --> C[数据库]
                        B -.->|数据同步| C
                        D[消息队列] --> B
                        D --> C
                        E[监控系统] --> B
                        E --> C
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#f59e0b,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#10b981,stroke:#fff,stroke-width:2px,color:#fff
                        style D fill:#ef4444,stroke:#fff,stroke-width:2px,color:#fff
                        style E fill:#6366f1,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
        </div>
    </section>

    <!-- Data Sync Strategies -->
    <section class="py-16 bg-white">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">
                <h2 class="text-4xl font-bold mb-12 section-title">数据同步策略</h2>
                
                <div class="grid md:grid-cols-2 gap-8 mb-12">
                    <div class="solution-card p-8 rounded-xl card-hover">
                        <div class="flex items-start space-x-4">
                            <div class="number-badge">A</div>
                            <div>
                                <h3 class="text-2xl font-bold mb-4">异步同步</h3>
                                <p class="text-gray-700 leading-relaxed">
                                    Redis和数据库之间采用异步同步，即应用程序在写入Redis后，不等待Redis同步完成，而是立即继续执行后续操作。这种方式下，可能会出现Redis中的数据和数据库中的数据不一致的情况，需要通过其他手段来解决这种不一致性。
                                </p>
                                <div class="mt-4 flex items-center space-x-4 text-sm text-gray-600">
                                    <span><i class="fas fa-bolt text-yellow-500"></i> 高性能</span>
                                    <span><i class="fas fa-exclamation-triangle text-orange-500"></i> 一致性风险</span>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="solution-card p-8 rounded-xl card-hover">
                        <div class="flex items-start space-x-4">
                            <div class="number-badge">B</div>
                            <div>
                                <h3 class="text-2xl font-bold mb-4">同步等待</h3>
                                <p class="text-gray-700 leading-relaxed">
                                    应用程序在写入Redis后，等待Redis同步完成，再执行后续操作。这种方式能够更好地保证一致性，但会增加响应时间，降低系统性能。
                                </p>
                                <div class="mt-4 flex items-center space-x-4 text-sm text-gray-600">
                                    <span><i class="fas fa-check-circle text-green-500"></i> 强一致性</span>
                                    <span><i class="fas fa-clock text-red-500"></i> 性能损耗</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Solutions Section -->
    <section class="py-16 bg-gray-50">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">
                <h2 class="text-4xl font-bold mb-12 section-title">解决方案</h2>
                
                <div class="space-y-8">
                    <!-- Solution 1 -->
                    <div class="solution-card p-8 rounded-xl card-hover">
                        <div class="flex items-start space-x-4">
                            <div class="number-badge">1</div>
                            <div class="flex-1">
                                <h3 class="text-2xl font-bold mb-4">使用事务</h3>
                                <p class="text-gray-700 leading-relaxed mb-4">
                                    在业务场景允许的情况下，可以将Redis的写操作和数据库的写操作放入一个事务中，确保它们要么同时成功，要么同时失败。这样可以保证Redis和数据库之间的写操作的一致性。
                                </p>
                                <div class="bg-gray-100 p-4 rounded-lg">
                                    <code class="text-sm text-purple-600">BEGIN TRANSACTION → Redis Write → DB Write → COMMIT/ROLLBACK</code>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Solution 2 -->
                    <div class="solution-card p-8 rounded-xl card-hover">
                        <div class="flex items-start space-x-4">
                            <div class="number-badge">2</div>
                            <div class="flex-1">
                                <h3 class="text-2xl font-bold mb-4">使用消息队列</h3>
                                <p class="text-gray-700 leading-relaxed mb-4">
                                    将Redis写操作和数据库写操作放入消息队列，通过消费者异步处理，保证一致性。例如，可以使用消息队列中间件如RabbitMQ、Kafka等。
                                </p>
                                <div class="grid grid-cols-3 gap-4 mt-6">
                                    <div class="text-center">
                                        <i class="fas fa-rabbit text-3xl text-orange-500 mb-2"></i>
                                        <p class="text-sm font-medium">RabbitMQ</p>
                                    </div>
                                    <div class="text-center">
                                        <i class="fas fa-stream text-3xl text-blue-500 mb-2"></i>
                                        <p class="text-sm font-medium">Kafka</p>
                                    </div>
                                    <div class="text-center">
                                        <i class="fas fa-rocket text-3xl text-red-500 mb-2"></i>
                                        <p class="text-sm font-medium">RocketMQ</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Solution 3 -->
                    <div class="solution-card p-8 rounded-xl card-hover">
                        <div class="flex items-start space-x-4">
                            <div class="number-badge">3</div>
                            <div class="flex-1">
                                <h3 class="text-2xl font-bold mb-4">数据变更通知</h3>
                                <p class="text-gray-700 leading-relaxed">
                                    当Redis中的数据发生变更时，通过触发器、发布订阅模式或其他机制，通知数据库进行相应的更新。这样可以实现Redis和数据库的实时同步。
                                </p>
                            </div>
                        </div>
                    </div>

                    <!-- Solution 4 -->
                    <div class="solution-card p-8 rounded-xl card-hover">
                        <div class="flex items-start space-x-4">
                            <div class="number-badge">4</div>
                            <div class="flex-1">
                                <h3 class="text-2xl font-bold mb-4">定期扫描对比</h3>
                                <p class="text-gray-700 leading-relaxed">
                                    定期扫描Redis和数据库中的数据，对比是否一致，如果发现不一致，进行修复。这种方式适用于实时性要求不是很高的场景。
                                </p>
                            </div>
                        </div>
                    </div>

                    <!-- Solution 5 -->
                    <div class="solution-card p-8 rounded-xl card-hover">
                        <div class="flex items-start space-x-4">
                            <div class="number-badge">5</div>
                            <div class="flex-1">
                                <h3 class="text-2xl font-bold mb-4">WATCH命令和乐观锁</h3>
                                <p class="text-gray-700 leading-relaxed mb-4">
                                    在Redis中，使用<code class="bg-gray-200 px-2 py-1 rounded text-purple-600">WATCH</code>命令和乐观锁机制，通过监视一个或多个键，确保在执行事务前这些键没有被其他客户端修